﻿using IdentityServer4.Models;
using IdentityServer4.Services;

namespace Demo.Net.IdentityServer.IdentityServer
{
    
    public class ProfileService : IProfileService
    {

        private readonly ILogger<ProfileService> _logger;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        public ProfileService(ILogger<ProfileService> logger)
        {
            _logger = logger;
        }


        /// <summary>
        /// 有关用户的身份信息单元被请求（例如在令牌创建期间或通过用户信息终结点），就会调用此方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            context.LogProfileRequest(_logger);

            context.IssuedClaims = context.Subject.Claims.ToList();

            context.LogIssuedClaims(_logger);

            return Task.CompletedTask;
        }

        /// <summary>
        /// 指示是否允许用户获取令牌的标志
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task IsActiveAsync(IsActiveContext context)
        {
            _logger.LogDebug("IsActive called from: {caller}", context.Caller);

            context.IsActive = true;

            return Task.CompletedTask;
        }
    }
}
